<?php
/**
 * Description: 错题上报
 * User: devforma
 * Date: 15/1/29
 * Time: 15:47
 */

class ReportErrorQuestion implements iController {
    private $newVersion = false;

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $cmsdb;
        
        $u = validateUser($queryArgs);
        if ($u == 1 || $u == 2) return genOutput(1000);

        //检测是否为新版
        if (!empty($queryArgs['app_version']) && version_cmp($queryArgs['app_version'], '1.1') >= 0)
            $this->newVersion = true;

        $date = date('Y-m-d H:i:s');

        if ($this->newVersion) {
            if (empty($queryArgs['question_id']) || empty($queryArgs['error_type']))
                return genOutput(1000);

            $existId = $cmsdb->getVar('SELECT `id` FROM `data_error_question` WHERE `question_id`=:qid',
                array(':qid' => $queryArgs['question_id']));

            switch ($queryArgs['error_type']) {
                case '1': //图文有错
                    if ($existId) {
                        $cmsdb->query("UPDATE `data_error_question` SET `content_error_count`=`content_error_count`+1,
                        `last_modified`='{$date}' WHERE `id`={$existId}");
                    } else {
                        $cmsdb->insert('data_error_question', array(
                            'question_id' => $queryArgs['question_id'],
                            'last_modified' => $date,
                            'content_error_count' => 1,
                            'answer_error_count' => 0,
                            'resolved' => 0
                        ));
                    }

                    if (!empty(trim($queryArgs['error_reason']))) {
                        $cmsdb->insert('data_ugc_question_analysis', array(
                            'user_id' => $queryArgs['user_id'],
                            'question_id' => $queryArgs['question_id'],
                            'analysis' => $queryArgs['error_reason'],
                            'type' => 1
                        ));
                    }

                    break;

                case '2': //答案有错
                case '3': //解析有错
                    if ($existId) {
                        $cmsdb->query("UPDATE `data_error_question` SET `answer_error_count`=`answer_error_count`+1,
                        `last_modified`='{$date}' WHERE `id`={$existId}");
                    } else {
                        $cmsdb->insert('data_error_question', array(
                            'question_id' => $queryArgs['question_id'],
                            'last_modified' => $date,
                            'content_error_count' => 0,
                            'answer_error_count' => 1,
                            'resolved' => 0
                        ));
                    }

                    if (!empty(trim($queryArgs['error_reason']))) {
                        $cmsdb->insert('data_ugc_question_analysis', array(
                            'user_id' => $queryArgs['user_id'],
                            'question_id' => $queryArgs['question_id'],
                            'analysis' => $queryArgs['error_reason'],
                            'type' => 1
                        ));
                    }

                    break;

                case '4': //更好的解析
                    if (!empty($queryArgs['my_analysis'])) {
                        $cmsdb->insert('data_ugc_question_analysis', array(
                            'user_id' => $queryArgs['user_id'],
                            'question_id' => $queryArgs['question_id'],
                            'analysis' => $queryArgs['my_analysis'],
                            'type' => 0
                        ));
                    }

                    break;

                default:
            }

        } else {
            if (empty($queryArgs['qid']) || !is_numeric($queryArgs['qid']))
                return genOutput(1000);

            $sql = "SELECT * FROM `data_error_question` WHERE `question_id`=:qid";
            $result = $cmsdb->getRow($sql, array(':qid' => $queryArgs['qid']));

            if (!empty($result)) { //有记录
                $sql = "UPDATE `data_error_question` SET `content_error_count`=`content_error_count`+1,
                `last_modified`='{$date}' WHERE `question_id`=:qid";
                $cmsdb->query($sql, array(':qid' => $queryArgs['qid']));

            } else { //无记录
                $cmsdb->insert('data_error_question', array(
                    'question_id' => $queryArgs['qid'],
                    'content_error_count' => 1,
                    'answer_error_count' => 0,
                    'last_modified' => $date,
                    'resolved' => 0
                ));
            }
        }
        
        return genOutput(1);
    }
}